package com.shop.order.interceptor;

import com.shop.constant.Const;
import com.shop.utils.CookieUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //执行handler之前执行此方法 true放行，false拦截
        String cookieValue = CookieUtils.getCookieValue(request, Const.TOKEN_LOGIN);
        //获取访问url
        String url = request.getRequestURI().toString();

        if(StringUtils.isBlank(cookieValue)){
            //跳转登录界面
            response.sendRedirect(request.getContextPath()+"/sso/login?returnUrl="+url);
            //拦截
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
